package com.ruoyi.biz.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.biz.domain.BizGameTag;
import com.ruoyi.biz.mapper.BizGameTagMapper;
import com.ruoyi.biz.pojo.dto.BizGameDTO;
import com.ruoyi.biz.pojo.dto.BizGameQueryDTO;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.biz.mapper.BizGameMapper;
import com.ruoyi.biz.domain.BizGame;
import com.ruoyi.biz.service.IBizGameService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 游戏信息Service业务层处理
 *
 * @author jason
 * @date 2025-10-31
 */
@Service
public class BizGameServiceImpl implements IBizGameService {
    @Autowired
    private BizGameMapper bizGameMapper;

    @Autowired
    BizGameTagMapper bizGameTagMapper;


    /**
     * 查询游戏信息
     *
     * @param id 游戏信息主键
     * @return 游戏信息
     */
    @Override
    public BizGame selectBizGameById(Long id) {
        return bizGameMapper.selectBizGameById(id);
    }

    /**
     * 查询游戏信息列表
     *
     * @param bizGame 游戏信息
     * @return 游戏信息
     */
    @Override
    public List<BizGame> selectBizGameList(BizGame bizGame) {
        return bizGameMapper.selectBizGameList(bizGame);
    }

    @Override
    public List<BizGame> listBizGame(BizGameQueryDTO query) {
        if (Objects.equals(query.getType(), 1)) {
            query.setOrderBy("create_time DESC ");
        } else if (Objects.equals(query.getType(), 2)) {
            query.setOrderBy("score DESC ");
        } else if (Objects.equals(query.getType(), 3)) {
            query.setOrderBy("bytes DESC ");
        } else if (Objects.equals(query.getType(), 4)) {

        }

        List<Long> tagIds = query.getTagIds();
        if (CollUtil.isNotEmpty(tagIds)) {
            List<Long> collect = bizGameTagMapper.listBizGameTagByTids(tagIds);
            if (CollUtil.isEmpty(collect)) {
                return Collections.emptyList();
            }
            query.setGameIds(new ArrayList<>(collect));
        }

        return bizGameMapper.listBizGame(query);
    }

    /**
     * 新增游戏信息
     *
     * @param bizGame 游戏信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertBizGame(BizGameDTO bizGame) {
        bizGame.setCreateTime(DateUtils.getNowDate());
        bizGame.setUpdateTime(DateUtils.getNowDate());
        bizGameMapper.insertBizGame(bizGame);
        handleTagBindRelation(bizGame);

        return 1;
    }

    private void handleTagBindRelation(BizGameDTO bizGame) {
        // 处理一下标签
        String tagsHidden = bizGame.getTagsHidden();
        if (StringUtils.isNotEmpty(tagsHidden)) {

            Set<Long> treeNodeIds = new HashSet<>(Arrays.asList(tagsHidden.split(","))).stream().map(Long::parseLong).collect(Collectors.toSet());
            if (Objects.nonNull(bizGame.getId())) {
                // 新增, 直接清除所有绑定, 然后重新绑定
                BizGameTag param = new BizGameTag();
                param.setGameId(bizGame.getId());
                List<BizGameTag> gameTags = bizGameTagMapper.selectBizGameTagList(param);
                for (BizGameTag gameTag : gameTags) {
                    bizGameTagMapper.deleteBizGameTagById(gameTag.getId());
                }
            }

            for (Long treeNodeId : treeNodeIds) {

                BizGameTag gameTag = new BizGameTag();
                gameTag.setGameId(bizGame.getId());
                gameTag.setTid(treeNodeId);
                gameTag.setCreateTime(DateUtils.getNowDate());
                bizGameTagMapper.insertBizGameTag(gameTag);
            }
        }
    }

    /**
     * 修改游戏信息
     *
     * @param bizGame 游戏信息
     * @return 结果
     */
    @Override
    public int updateBizGame(BizGameDTO bizGame) {
        bizGame.setUpdateTime(DateUtils.getNowDate());

        handleTagBindRelation(bizGame);
        return bizGameMapper.updateBizGame(bizGame);
    }

    /**
     * 批量删除游戏信息
     *
     * @param ids 需要删除的游戏信息主键
     * @return 结果
     */
    @Override
    public int deleteBizGameByIds(String ids) {
        return bizGameMapper.deleteBizGameByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除游戏信息信息
     *
     * @param id 游戏信息主键
     * @return 结果
     */
    @Override
    public int deleteBizGameById(Long id) {
        return bizGameMapper.deleteBizGameById(id);
    }

    @Override
    public List<BizGame> listByIds(List<Long> ids) {
        return bizGameMapper.listByIds(ids);
    }
}
